package lookie.swing.tree.model;

import javax.swing.tree.TreeNode;

import lookie.swing.tree.model.util.DeferredTreeNodeLoader;

public class DeferredTreeNodeModel extends LazyTreeNodeModel {
	
	private static final long serialVersionUID = -1380444803949274359L;

	public DeferredTreeNodeModel(TreeNode root, TreeNodeProvider provider) {
		super(root, provider);
	}
	
	@Override
	public int getChildCount(Object parent) {
		if (parent instanceof DeferredTreeNode) {
			DeferredTreeNode node = (DeferredTreeNode) parent;
			if (node.isDeferred() && !node.isLoaded()) {
				DeferredTreeNodeLoader loader = new DeferredTreeNodeLoader(getProvider()) {

					@Override
					public void postDeferred(DeferredTreeNode parent) {
						nodeStructureChanged(parent);
					}
					
				};
				loader.load(node);
			}
		}
		return super.getChildCount(parent);
	}

}
